package com.zk.nakedfowl.basicData.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.*;

import com.zk.nakedfowl.basicData.pojo.Card;
import com.zk.nakedfowl.basicData.pojo.House;
import com.zk.nakedfowl.basicData.pojo.Stalls;
import com.zk.nakedfowl.common.util.IdWorker;
import com.zk.nakedfowl.powerManager.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;


import com.zk.nakedfowl.basicData.dao.MachineDao;
import com.zk.nakedfowl.basicData.pojo.Machine;

/**
 * 服务层
 * 
 * @author Administrator
 *
 */
@Service
public class MachineService {

	@Autowired
	private MachineDao machineDao;
	
	@Autowired
	private IdWorker idWorker;

	/**
	 * 查询全部列表
	 * @return
	 */
	public List<Machine> findAll() {
		return machineDao.findAll();
	}

	
	/**
	 * 条件查询+分页
	 * @param whereMap
	 * @param page
	 * @param size
	 * @return
	 */
	public Page<Machine> findSearch(Map whereMap, int page, int size) {
		Specification<Machine> specification = createSpecification(whereMap);
		PageRequest pageRequest =  PageRequest.of(page-1, size);
		return machineDao.findAll(specification, pageRequest);
	}

	
	/**
	 * 条件查询
	 * @param whereMap
	 * @return
	 */
	public List<Machine> findSearch(Map whereMap) {
		Specification<Machine> specification = createSpecification(whereMap);
		return machineDao.findAll(specification);
	}

	/**
	 * 根据ID查询实体
	 * @param id
	 * @return
	 */
	public Machine findById(String id) {
		return machineDao.findById(id).get();
	}

	/**
	 * 增加
	 * @param machine
	 */
	public void add(Machine machine) {
		machine.setId( idWorker.nextId()+"" );
		machineDao.save(machine);
	}

	/**
	 * 修改
	 * @param machine
	 */
	public void update(Machine machine) {
		machineDao.save(machine);
	}

	/**
	 * 删除
	 * @param id
	 */
	public void deleteById(String id) {
		machineDao.deleteById(id);
	}

	public Object findByIdName(String id, String createBy) {


		Map<String,String> m=new HashMap();
		m.put("id",id);
		m.put("createBy",createBy);

		Specification<Machine> specification = createSpecification(m);
		return machineDao.findAll(specification);
	}



	/**
	 * 动态条件构建
	 * @param searchMap
	 * @return
	 */
	private Specification<Machine> createSpecification(Map searchMap) {

		return new Specification<Machine>() {

			@Override
			public Predicate toPredicate(Root<Machine> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> predicateList = new ArrayList<Predicate>();
                // 
                if (searchMap.get("id")!=null && !"".equals(searchMap.get("id"))) {
                	predicateList.add(cb.equal(root.get("id").as(String.class), (String)searchMap.get("id")));
                }
                // 脚标机编号
                if (searchMap.get("number")!=null && !"".equals(searchMap.get("number"))) {
                	predicateList.add(cb.equal(root.get("number").as(String.class), (String)searchMap.get("number")));
                }
                // 所属的屠宰场id
                if (searchMap.get("slaughterHouseId")!=null && !"".equals(searchMap.get("slaughterHouseId"))) {
                	predicateList.add(cb.equal(root.get("slaughterHouseId").as(String.class), (String)searchMap.get("slaughterHouseId")));
                }
				// 所属的档口id
				if (searchMap.get("stallsId")!=null && !"".equals(searchMap.get("stallsId"))) {
					predicateList.add(cb.equal(root.get("stallsId").as(String.class), (String)searchMap.get("stallsId")));
				}
                // 档口负责人（档口的使用者）
                if (searchMap.get("userId")!=null && !"".equals(searchMap.get("userId"))) {
                	predicateList.add(cb.equal(root.get("userId").as(String.class), (String)searchMap.get("userId")));
                }
                // 经度
                if (searchMap.get("longitude")!=null && !"".equals(searchMap.get("longitude"))) {
                	predicateList.add(cb.equal(root.get("longitude").as(String.class), (String)searchMap.get("longitude")));
                }
                // 纬度
                if (searchMap.get("latitude")!=null && !"".equals(searchMap.get("latitude"))) {
                	predicateList.add(cb.equal(root.get("latitude").as(String.class), (String)searchMap.get("latitude")));
                }
                // 创建者
                if (searchMap.get("createBy")!=null && !"".equals(searchMap.get("createBy"))) {
                	predicateList.add(cb.equal(root.get("createBy").as(String.class), (String)searchMap.get("createBy")));
                }


				return cb.and( predicateList.toArray(new Predicate[predicateList.size()]));

			}
		};

	}


}
